import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:open_file/open_file.dart';

import '../../helper/downloadCBH.dart';
import '../../helper/downloadHelper.dart';

import '../../model/appversion.dart';

class AppUpdatePage extends StatefulWidget {
  @override
  AppUpdatePage(this.s1);
  final AppVersion s1;

  @override
  AppUpdatePageState createState() => new AppUpdatePageState();
}

class AppUpdatePageState extends State<AppUpdatePage> {
  double pro1 = 0.0;
  bool loading = false;

  @override
  void initState() {
    // TODO: implement initState

    super.initState();
  }

  void setValue(String id, int progress) async {
    setState(() {
      pro1 = progress.toDouble() / 100;
    });

    if (progress == 100) {
      var xx = await DownloadHelper.GetLocalFile(widget.s1.FileId);
      //FlutterDownloader.open(taskId:id);
      OpenFile.open(xx);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //backgroundColor: Colors.white,

        body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("lib/assets/images/bg.png"),
                fit: BoxFit.cover,
              ),
              color: Colors.transparent,
            ),
            child: Center(
                child: CupertinoAlertDialog(
              title: Text("发现新版本"),
              content: Column(children: <Widget>[
                SizedBox(
                  height: 20,
                ),
                Align(
                  child: Text(
                    widget.s1.Debug,
                    textAlign: TextAlign.start,
                    style: TextStyle(
                      fontSize: 14.0,
                      height: 1.5,
                    ),
                  ),
                  alignment: Alignment(0, 0),
                ),
                SizedBox(
                  height: 20,
                ),
                SizedBox(
                  child: loading == true
                      ? LinearProgressIndicator(
                          value: pro1,
                        )
                      : Container(),
                  height: 4.0,
                  // width: 200,
                ),
              ]),
              actions: <Widget>[
                CupertinoDialogAction(
                  child: Text("立即更新"),
                  onPressed: () async {
                    var fpath =
                        await DownloadHelper.GetLocalFile(widget.s1.FileId);
                    if (fpath != "") {
                      OpenFile.open(fpath);
                      return;
                    }

                    if (!loading) {
                      setState(() {
                        loading = true;
                      });

                      var id = await DownloadHelper.DownLoadByGuid(
                          context, widget.s1.FileId, ".apk");

                      print(id);
                      FileLoadProgress().on(id!, setValue);
                    }
                  },
                ),
              ],
            ))));
  }
}
